package server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Random;
import java.util.regex.Pattern;

import common.Constants;
import common.Map;
import common.Tools;

public class SubServer implements Runnable {

	private Socket socket;
	private ObjectOutputStream writer;
	private ObjectInputStream reader;
	private ObjectOutputStream writerr;
	private ObjectInputStream readerr;
	private Tools ForPlayer = new Tools();
	private Tools ForPlayerr = new Tools();
	private String Msg;


	public void setSocket(Socket socket) {
		this.socket = socket;

		try {

			if (Thread.currentThread().getName().equals("0")) {
				ForPlayer.manageShip();
				writer = new ObjectOutputStream(socket.getOutputStream());
				reader = new ObjectInputStream(socket.getInputStream());
				run();
				receiveMap();

				System.out.print("thread 0 finished");
			} else {
				ForPlayerr.manageShip();
				writerr = new ObjectOutputStream(socket.getOutputStream());
				readerr = new ObjectInputStream(socket.getInputStream());
				run();
				receiveMap();
				System.out.print("thread 1 finished");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public synchronized void receiveMap() throws IOException, ClassNotFoundException,InterruptedException {
		if(Thread.currentThread().getName().equals("0")){
			writer.writeObject("1");
			writer.flush();
		}else{
			writerr.writeObject("1");
			writerr.flush();
		}
	

		while (Constants.isRunning) {

					if(Thread.currentThread().getName().equals("0")){
					System.out.println(Thread.currentThread().getName()+"*OOOOOOOOOO");
					Msg = (String) reader.readObject();
					System.out.print("here");
					System.out.print(Msg);
					writerr.writeObject("1");
			}else{
				    
					System.out.println(Thread.currentThread().getName()+"*OOOOOOOOOO");
					Msg = (String) readerr.readObject();
					System.out.print(Msg);
					writer.writeObject("1");
				}
			
			System.out.print(Msg);
			dataTransfer();
			this.notify();
			this.wait();
		}
	}

	public synchronized void sendMap() throws IOException, ClassNotFoundException {

		if (Thread.currentThread().getName().equals("0")) {
			Server.mapReceive = ForPlayer.getArray();
			writer.writeObject(Server.mapReceive);
			writer.flush();
		
			for (int i = 0; i < 6; i++) {
				for (int j = 0; j < 6; j++) {
					System.out.print(Server.mapReceive[i][j]);
				}
				System.out.println();
			}
			System.out.println("sendmap to client thread "
					+ Thread.currentThread().getName() + "^^^^^");

		} else if (Thread.currentThread().getName().equals("1")) {
			Server.mapReceivee = ForPlayerr.getArray();
			writerr.writeObject(Server.mapReceivee);
			writerr.flush();
		

			for (int i = 0; i < 6; i++) {
				for (int j = 0; j < 6; j++) {
					System.out.print(Server.mapReceivee[i][j]);
				}
				System.out.println();
			}
			System.out.println("sendmap to client..thread "
					+ Thread.currentThread().getName() + "^^^^^^");
		}
	}

	public synchronized String[] processMsg(String str) {
		String[] r = str.split("");
		return r;
	}

	public synchronized void dataTransfer() throws IOException {     //synchronized

		if (Thread.currentThread().getName().equals("0")){
			String[] str = processMsg(Msg);
			if (Server.mapReceivee[Integer.parseInt(str[1])][Integer.parseInt(str[2])].equals(" ")) {

				sendNormalMsg(Msg + "0"); 
				writerr.writeObject(Msg + "00");
				writerr.flush();

			} else {
				
				Server.countA++;
				if(Server.countA==10){
					sendNormalMsg("9");
				}
				
				if(Server.mapReceivee[Integer.parseInt(str[1])][Integer.parseInt(str[2])].equals("b"))    //b is 1;c is 2; d is 3;s is 4
				{sendNormalMsg(Msg + "1");
				writerr.writeObject(Msg + "10");
				writerr.flush();}
				else if(Server.mapReceivee[Integer.parseInt(str[1])][Integer.parseInt(str[2])].equals("c")){
					{sendNormalMsg(Msg + "2");
					writerr.writeObject(Msg + "20");
					writerr.flush();}
				}else if(Server.mapReceivee[Integer.parseInt(str[1])][Integer.parseInt(str[2])].equals("d")){
					sendNormalMsg(Msg + "3");
					writerr.writeObject(Msg + "30");
					writerr.flush();
				}else if(Server.mapReceivee[Integer.parseInt(str[1])][Integer.parseInt(str[2])].equals("s")){
					sendNormalMsg(Msg + "4");
					writerr.writeObject(Msg + "40");
					writerr.flush();
				}
				
				
			//	sendNormalMsg(Msg + "1");
			//	writerr.writeObject(Msg + "10");
			//	writerr.flush();

			}
		} else if (Thread.currentThread().getName().equals("1")){
			String[] str = processMsg(Msg);
			if (Server.mapReceive[Integer.parseInt(str[1])][Integer.parseInt(str[2])].equals(" ")) {

				sendNormalMsg(Msg + "0"); // no bingo
				writer.writeObject(Msg + "00");
				writer.flush();

			} else {
				Server.countB++;
				if(Server.countB==10){
					sendNormalMsg("9");
				}
				
				if(Server.mapReceive[Integer.parseInt(str[1])][Integer.parseInt(str[2])].equals("b"))    //b is 1;c is 2; d is 3;s is 4
				{sendNormalMsg(Msg + "1");
				writer.writeObject(Msg + "10");
				writer.flush();}
				else if(Server.mapReceive[Integer.parseInt(str[1])][Integer.parseInt(str[2])].equals("c")){
					{sendNormalMsg(Msg + "2");
					writer.writeObject(Msg + "20");
					writer.flush();}
				}else if(Server.mapReceive[Integer.parseInt(str[1])][Integer.parseInt(str[2])].equals("d")){
					sendNormalMsg(Msg + "3");
					writer.writeObject(Msg + "30");
					writer.flush();
				}else if(Server.mapReceive[Integer.parseInt(str[1])][Integer.parseInt(str[2])].equals("s")){
					sendNormalMsg(Msg + "4");
					writer.writeObject(Msg + "40");
					writer.flush();
				}

			}
		}
	}

	public synchronized void sendNormalMsg(String str) throws IOException {
		if(Thread.currentThread().getName().equals("0")){
			writer.writeObject(str);
			writer.flush();
		}else{
			writerr.writeObject(str);
			writerr.flush();
		}
	}



	@Override
	public  void run() {
		try {
			sendMap();
			System.out.println("sever side received msg in runnnnnnnn....");
			// receiveMap();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
